package com.tulele.vtool.handler;

import com.tulele.vtool.model.AdvertInfo;
import com.tulele.vtool.model.AdvertTagInfo;
import com.tulele.vtool.model.ArticleAdvertInfo;
import com.tulele.vtool.model.ArticleInfo;
import com.tulele.vtool.model.reqEntity.HeadlineReqBean;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.AdvertInfoService;
import com.tulele.vtool.service.HeadlineService;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class HeadlineHandler {

    @Autowired
    private HeadlineService headlineService;

//    @Value("${spring.profiles.active}")
//    private String springProfilesActive;


    /**
     * 头条首页
     * @param userId
     * @return
     */
    @PostMapping(value = "/selectHeadlineByGzhCode", produces = "application/json;charset=UTF-8")
    public RespData selectHeadlineByGzhCode(@RequestHeader("userId") String userId,@RequestHeader("identifying") String springProfilesActive) {
        Map<String,List<Object>> map  = this.headlineService.selectHeadlineByGzhCode(springProfilesActive);
        RespData respData = new RespData();
        if (map.isEmpty()){
            respData.setCode(ReturnCode.SERVER_ERROR);
            respData.setData(1);
            respData.setMsg("暂无数据");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(map);
        respData.setMsg("正常");
        return respData;
    }

    /**
     * 通过头条标题分页查询文章
     * @param userId
     * @param headlineReqBean
     * @return
     */
    @PostMapping(value = "/selectArticleInfoByHeadlineTitleId", produces = "application/json;charset=UTF-8")
    public RespData selectHeadlineByGzhCode(@RequestHeader("userId") String userId,@RequestBody HeadlineReqBean headlineReqBean) {
        Integer pageNum = headlineReqBean.getPageNum();
        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = 1;
        }

        Integer pageSize = headlineReqBean.getPageSize();
        if (pageSize == null || pageSize.intValue() <= 0) {
            pageSize = 10;
        }
        List<ArticleInfo> articleInfos =this.headlineService.selectArticleInfoByHeadlineTitleId(pageNum,pageSize,headlineReqBean.getHeadlineTitleId());
        RespData respData = new RespData();
        if (CollectionUtils.isEmpty(articleInfos)){
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setMsg("暂无数据");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(articleInfos);
        respData.setMsg("正常");
        return respData;
    }








}
